package com.maiji.cloud.controller;

import com.maiji.cloud.entities.login.AppUser;
import com.maiji.cloud.entities.login.ShareWeightRuleEntity;
import com.maiji.cloud.request.BaseDataReqDto;
import com.maiji.cloud.request.BaseInputDto;
import com.maiji.cloud.request.IdInputDto;
import com.maiji.cloud.request.ReqMetaData;
import com.maiji.cloud.request.login.*;
import com.maiji.cloud.request.shopingmall.DistributionLogListReqDto;
import com.maiji.cloud.response.BaseMetaResDto;
import com.maiji.cloud.response.Status;
import com.maiji.cloud.response.login.*;
import com.maiji.cloud.service.AppUserService;
import com.maiji.cloud.service.LoginLogService;
import com.maiji.cloud.service.ShareWeightService;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.fastjson.JSON;
import com.maiji.cloud.response.BaseDataResDto;
import com.maiji.cloud.response.BaseResDto;
import com.maiji.cloud.service.UserService;
import com.maiji.cloud.utils.WXUtil;

@RestController
@RequestMapping("/user/")
public class UserController {

	private static final Logger logger = LoggerFactory.getLogger(UserController.class);
	@Autowired
	private UserService userService;
	@Autowired
	private LoginLogService loginLogService;
	@Autowired
	private ShareWeightService shareWeightService;
	@Autowired
	private AppUserService appUserService; 
	
	/**
	 * 记录体重获取减肥基金规则设置
	 * @param param
	 * @return
	 */
	@PostMapping("addShareWeightRuleInfo")
	public BaseResDto addShareWeightRuleInfo(@RequestBody BaseDataReqDto<ShareWeightRuleEntity> param){
		
		return shareWeightService.addShareWeightRuleInfo(param);
	}
	
	/**
	 * 第一次使用app时设置个人基本信息
	 * @param setUserInfoReqDto
	 * @return
	 */
	@PostMapping("setingUserInfo")
	public BaseResDto setingUserInfo(@RequestBody SetUserInfoReqDto setUserInfoReqDto )throws Exception{
		logger.info("UserController.setingUserInfo param is {}",JSON.toJSONString(setUserInfoReqDto));
		return userService.setingUserInfo(setUserInfoReqDto);
	}
	
	/**
	 * 结束麦吉之旅
	 * @param maijiToken
	 * @return
	 */
	@PostMapping("endMaiji")
	public BaseResDto endMaiji(@RequestHeader("maijiToken")String maijiToken){
		
		// 获得用户id
		AppUser appUser = appUserService.checkToken(maijiToken);

		if (appUser == null) {
			logger.info("UserController.endMaiji,appUser is {}", JSON.toJSONString(appUser));
			return BaseResDto.baseResDto(Status.PARAMETERERROR, "token 无效 ");
		}
		return userService.endMaiji(appUser.getUuId());
	}

    /**
     *  修改用户信息
     * @param updateUserInfoReqDto
     * @return
     */
	@PostMapping("updateUserInfo")
	public BaseResDto updateUserInfo(@RequestBody UpdateUserInfoReqDto updateUserInfoReqDto )throws Exception{
		logger.info("UserController.updateUserInfo param is {}",JSON.toJSONString(updateUserInfoReqDto));
		return userService.updateUserInfo(updateUserInfoReqDto);
	}
	
	/**
	 * 获取弹框信息
	 * @return
	 */
	@PostMapping("getUserStageMessageInfo")
	public BaseDataResDto<MessageInfoResData> getUserStageMessageInfo(@RequestHeader("maijiToken")String maijiToken)throws Exception{
		logger.info("UserController.getUserStageMessageInfo maijiToken is {}",maijiToken);
		return userService.getUserStageMessageInfo(maijiToken);
	}

	/**
	 * 确认阶段弹框操作
	 * @param param
	 * @return
	 */
	@PostMapping("confirmMessageOperation")
	public BaseDataResDto<String> confirmMessageOperation(@RequestBody BaseDataReqDto<Integer> param,@RequestHeader("maijiToken")String maijiToken){
		logger.info("UserController.updateUserInfo param is {}",JSON.toJSONString(param));
		return userService.confirmMessageOperation(param,maijiToken);
	} 
	
	/**
	 * 记录阶段
	 * @param maijiToken
	 * @param param
	 * @return
	 */
	@PostMapping("recordStage")
	public BaseResDto recordStage(@RequestHeader("maijiToken")String maijiToken,@RequestBody BaseDataReqDto<RecordStageReqDataDto> param )throws Exception{
		logger.info("UserController.recordStage param is {}",JSON.toJSONString(param));
		return userService.recordStage(param,maijiToken);
	}

	/**
	 * 获得进入app设置的个人信息
	 * @param maijiToken
	 * @return
	 */
	@PostMapping("getUserInfo")
	public UserInfoResDto getUserInfo(@RequestHeader("maijiToken")String maijiToken )throws Exception{
		logger.info("UserController.getUserInfo param is {}",maijiToken);
		return userService.getUserInfo(maijiToken);
	}

	/**
	 * 获取用户基本信息
	 * @param
	 * @return
	 * @throws Exception 
	 */
	@PostMapping("getUserInfoWithUserId")
	public UserInfoResDto getUserInfoWithUserId(@RequestBody BaseInputDto<IdInputDto> inputDto) throws Exception{

		return userService.getUserInfoWithUserId(inputDto);
	}
	
	/**
	 * 记录当前体重
	 * @param recordWeightReqDto
	 * @return
	 * @throws Exception 
	 */
	@PostMapping("recordWeight")
	public BaseDataResDto<RecordWeightResData> recordWeight(@RequestBody RecordWeightReqDto recordWeightReqDto) throws Exception{
		logger.info("UserController.recordWeight,recordWeight ,recordWeightReqDto is {}",JSON.toJSONString(recordWeightReqDto));
		
		return userService.recordWeight( recordWeightReqDto);
	}

	/**
	 * 获得一个用户的所有的体重记录 
	 * @param maijiToken
	 * @return
	 */
	@PostMapping("getWeightChangeLogList")
	public WeightChangeLogListResDto getWeightChangeLogList(@RequestHeader("maijiToken")String maijiToken){
		logger.info("UserController.getWeightChangeLogList ,maijiToken is {}",maijiToken);
		
		return userService.getWeightChangeLogList(maijiToken);
	}

	/**
	 * 获取尿酮
	 * @param maijiToken
	 * @return
	 */
	@PostMapping("getKetonuria")
	public BaseDataResDto<List<WeightChangeLogResData>> getKetonuria(@RequestBody BaseDataReqDto<GetKetonuriaResData> param,
                                                                     @RequestHeader("maijiToken")String maijiToken)throws Exception{
		
		return userService.getKetonuria(param,maijiToken);
	}

	/**
	 * 记录尿酮
	 * @param param
	 * @param maijiToken
	 * @return
	 * @throws Exception
	 */
	@PostMapping("recordKetonuria")
	public BaseResDto recordKetonuria(@RequestBody BaseDataReqDto<String> param ,@RequestHeader("maijiToken")String maijiToken) throws Exception{
		return userService.recordKetonuria(param,maijiToken);
	}
	
	@PostMapping("uploadLocation")
	public BaseResDto uploadLocation (@RequestBody BaseDataReqDto<UploadLocationReqDto > baseDataReqDto, @RequestHeader("maijiToken") String maijiToken) {
        UploadLocationReqDto data = baseDataReqDto.getData();
        return loginLogService.uploadLocation(data, maijiToken);
	}

	/**
	 * 用户签到
	 * @param maijiToken
	 * @return
	 */
	@PostMapping("userSignIn")
	public BaseDataResDto<UserSignListResData> userSignIn(@RequestHeader("maijiToken") String maijiToken)throws Exception{
		logger.info("UserController.userSignIn ,maijiToken is {}",maijiToken);
		
		return userService.userSignIn(maijiToken);
	}
	
	//今天是否签到
	@PostMapping("todaySignInResult")
	public BaseDataResDto<Boolean> todaySignInResult(@RequestHeader("maijiToken") String maijiToken) throws Exception{
		logger.info("UserController.todaySignInResult ,maijiToken is {}",maijiToken);
		
		return userService.todaySignInResult(maijiToken);
	}

	/**
	 *  获取用户签到
	 * @param maijiToken
	 * @return
	 */
	@PostMapping("getUserSignIn")
	public UserSignListResDto getUserSignIn(@RequestHeader("maijiToken") String maijiToken) throws Exception{
		logger.info("UserController.getUserSignIn ,maijiToken is {}",maijiToken);
		
		return userService.getUserSignIn(maijiToken);
	}
	
	@PostMapping("getSignInDeployAll")
	public SignInDeployAllResDto getSignInDeployAll(@RequestHeader("maijiToken") String maijiToken) {
		
		return userService.getSignInDeployAll();
	}

	/**
	 * 转发
	 * @param param
	 * @return
	 */
	@PostMapping("forwardingRecords")
	public BaseResDto forwardingRecords(@RequestBody ForwardingRecordsReqDto param,@RequestHeader("maijiToken") String maijiToken){
		logger.info("UserController.forwardingRecords ,param is {},maijiToken is {}",JSON.toJSONString(param),maijiToken);
		
		return userService.forwardingRecords(param,maijiToken);
	}
	
	/**
	 * 获得转发记录
	 * @param param
	 * @return
	 */
	@PostMapping("getforwardingRecordsList")
	public ForwardingRecordsListResDto getforwardingRecordsList(@RequestBody BaseDataReqDto param, @RequestHeader("maijiToken") String maijiToken){
		logger.info("UserController.getforwardingRecordsList ,param is {},maijiToken is {}",JSON.toJSONString(param),maijiToken);
		
		return userService.getforwardingRecordsList(param,maijiToken);
	}

	/**
	 * 微信用户助力
	 * @param param
	 * @return
	 */
	@PostMapping("forwardingRecordsHelp")
	public BaseResDto forwardingRecordsHelp(@RequestBody ForwardingRecordsHelpReqDto param){
		logger.info("UserController.forwardingRecordsHelp ,param is {}",JSON.toJSONString(param));
		
		return userService.forwardingRecordsHelp(param);
	}

	/**
	 * 统计分销用户数量
	 * @param param
	 * @param maijiToken
	 * @return
	 */
	@PostMapping("getDistributionSum")
	public DistributionSumResDto getDistributionSum(@RequestBody DistributionLogListReqDto param, @RequestHeader(name = "maijiToken") String maijiToken) {
		logger.info("UserController.getDistributionSum ,param is {}",JSON.toJSONString(param));
		
		return userService.getDistributionSum(param,maijiToken);
	}

	/**
	 * 获取今日分销统计用户列表
	 * @param param
	 * @param maijiToken
	 * @return
	 */
	@PostMapping("getDistributionUserTodayList")
	public DistributionUserListResDto getDistributionUserTodayList(@RequestBody DistributionLogListReqDto param, @RequestHeader(name = "maijiToken") String maijiToken){
		logger.info("UserController.getDistributionUserList ,param is {},maijiToken is {}",JSON.toJSONString(param),maijiToken);
		param.getData().setType(2);
		return userService.getDistributionUserList(param,maijiToken);
	}
	
	/**
	 *获取所有分销统计用户列表
	 * @param param
	 * @param maijiToken
	 * @return
	 */
	@PostMapping("getDistributionUserAllList")
	public DistributionUserListResDto getDistributionUserAllList(@RequestBody DistributionLogListReqDto param, @RequestHeader(name = "maijiToken") String maijiToken){
		logger.info("UserController.getDistributionUserList ,param is {},maijiToken is {}",JSON.toJSONString(param),maijiToken);
		param.getData().setType(1);
		return userService.getDistributionUserList(param,maijiToken);
	}

	/**
	 * 分享体重用户助力
	 * @param param
	 * @param maijiToken
	 * @return
	 */
	@PostMapping("shareWeightUserHelp")
	public BaseDataResDto<Boolean> shareWeightUserHelp(@RequestBody BaseDataReqDto<String> param, @RequestHeader("maijiToken") String maijiToken){
		logger.info("UserController.shareWeightUserHelp ,param is {},maijiToken is {}",JSON.toJSONString(param),maijiToken);
		return shareWeightService.shareWeightUserHelp(param,maijiToken);
	}

	/**
	 * 获得减肥基金明细
	 * @param param
	 * @param maijiToken
	 * @return
	 */
	@PostMapping("getReduceWeightFundLogList")
	public BaseDataResDto<List<ReduceWeightFundLogResData>> getReduceWeightFundLogList(@RequestBody BaseDataReqDto<ReduceWeightFundLogReqData> param, @RequestHeader("maijiToken") String maijiToken){
		logger.info("UserController.getReduceWeightFundLogList ,param is {},maijiToken is {}",JSON.toJSONString(param),maijiToken);
		return shareWeightService.getReduceWeightFundLogList(param,maijiToken);
	}

	/**
	 * 体重分享之后点击查看详情
	 * @param param
	 * @return
	 */
	@PostMapping("getShareWeightDetails")
	 public BaseDataResDto<ShareWeightDetailsData> getShareWeightDetails(@RequestBody BaseDataReqDto<String> param,@RequestHeader(value="maijiToken",required =false) String maijiToken){
		logger.info("UserController.getShareWeightDetails ,param is {}",JSON.toJSONString(param));
		if(StringUtils.isBlank(param.getData())) {
			return new BaseDataResDto<ShareWeightDetailsData>(Status.PARAMETERERROR,"参数为空");
		}
		return shareWeightService.getShareWeightDetails(param,maijiToken);
	}

	/**
	 * 获取助力用户列表
	 * @param param
	 * @return
	 */
	@PostMapping("getShareWeightUserHelpList")
	public BaseDataResDto<List<ShareWeightUserHelpList>> getShareWeightUserHelpList(@RequestBody BaseDataReqDto<String> param){
		logger.info("UserController.getShareWeightUserHelpList ,param   is {}",JSON.toJSONString(param));
		if(StringUtils.isBlank(param.getData())) {
			return new BaseDataResDto<List<ShareWeightUserHelpList>>(Status.PARAMETERERROR,"参数为空");
		}
		
		return shareWeightService.getShareWeightUserHelpList(param);
		
	}

    /**
     * 获取用户列表
     * @param
     * @return
     */
    @PostMapping("findAllMaijiUsers")
    public BaseMetaResDto<List<UserInfoResData>> findAllMaijiUsers(@RequestBody BaseDataReqDto<MaijiUserFindAllReqData> baseDataReqDto){
        MaijiUserFindAllReqData data = baseDataReqDto.getData();
        ReqMetaData metaData = baseDataReqDto.getMetaData();
        return userService.findAllMaijiUsers(data, metaData);
    }
	
	/**
	 * 分销获得上级用户信息
	 * @param param
	 * @return
	 */
	@PostMapping("getAppUer")
	public BaseDataResDto<AppUser> getAppUer(@RequestBody BaseDataReqDto<String> param){
		logger.info("UserController.getAppUer ,param is {}",JSON.toJSONString(param));
		if(StringUtils.isBlank(param.getData())) {
			return new BaseDataResDto<AppUser>(Status.PARAMETERERROR,"参数为空");
		}
		return userService.getAppUer(param);
	}
	
	/**
	 * 查看当天是否有正在助力的减肥基金
	 * @param maijiToken
	 * @return
	 */
	@PostMapping("isHaveReduceWeightFundLog")
	public  BaseDataResDto<IsHaveReduceWeightFundLogResData> isHaveReduceWeightFundLog(@RequestHeader("maijiToken") String maijiToken)throws Exception{
		logger.info("UserController.isHaveReduceWeightFundLog ,maijiToken is {}",maijiToken);
		return userService.isHaveReduceWeightFundLog(maijiToken);
	}
	
	/**
	 * 用户领取减肥基金
	 * @param maijiToken
	 * @return
	 * @throws Exception
	 */
	@PostMapping("userDrawReduceWeightFund")
	public BaseDataResDto<RecordWeightResData> userDrawReduceWeightFund(@RequestBody BaseDataReqDto<String> param,@RequestHeader("maijiToken") String maijiToken)throws Exception{
		logger.info("UserController.userDrawReduceWeightFund ,param is {}",JSON.toJSONString(param));
		return userService.userDrawReduceWeightFund(param,maijiToken);
	}
	
	@PostMapping("testWX")
	public  BaseDataResDto<Map> testWX(@RequestHeader("maijiToken") String maijiToken)throws Exception{
		Map map = WXUtil.wxWithdraw(null,null,null);
		logger.info("UserController.testWX ,map is {}",JSON.toJSONString(map));
		return new BaseDataResDto<Map>().setData(map);
	}

	/**
	 * 设置支付密码
	 * @param param
	 * @param maijiToken
	 * @return
	 */
	@PostMapping("setPayPassword")
	public BaseResDto setPayPassword(@RequestBody BaseDataReqDto<SetPayPasswordReqData> param,@RequestHeader("maijiToken") String maijiToken){
		logger.info("UserController.setPayPassword ,param is {},maijiToken is {}", JSON.toJSONString(param),maijiToken);
		SetPayPasswordReqData data = param.getData();
		if(StringUtils.isBlank(maijiToken) || StringUtils.isBlank(data.getIdCard()) || StringUtils.isBlank(data.getPayPassword()) || StringUtils.isBlank(data.getRealName()))return new BaseResDto(Status.PARAMETERERROR, "参数为空");
		// 获得用户id
		AppUser appUser = null;
		appUser = appUserService.checkToken(maijiToken);
		if (appUser == null) {
			logger.info("UserController.setPayPassword,appUser is {}", JSON.toJSONString(appUser));
			return new BaseResDto(Status.PARAMETERERROR, "token 无效 ");
		}
		return userService.setPayPassword(param,appUser.getUuId());
	}
	
	/**
	 * 修改支付密码
	 * @param param
	 * @param maijiToken
	 * @return
	 */
	@PostMapping("updatePayPassword")
	public BaseResDto updatePayPassword(@RequestBody BaseDataReqDto<UpdatePayPasswordReqData> param,@RequestHeader("maijiToken") String maijiToken){
		logger.info("UserController.updatePayPassword ,param is {},maijiToken is {}", JSON.toJSONString(param),maijiToken);
		UpdatePayPasswordReqData data = param.getData();
		if(StringUtils.isBlank(maijiToken) || StringUtils.isBlank(data.getOldPayPassword()) || StringUtils.isBlank(data.getNewPayPassword()))return new BaseResDto(Status.PARAMETERERROR, "参数为空");
		// 获得用户id
		AppUser appUser = null;
		appUser = appUserService.checkToken(maijiToken);
		if (appUser == null) {
			logger.info("UserController.updatePayPassword,appUser is {}", JSON.toJSONString(appUser));
			return new BaseResDto(Status.PARAMETERERROR, "token 无效 ");
		}
		return userService.updatePayPassword(param,appUser.getUuId());
	}

	/**
	 * 校验支付密码是否正确
	 * @param param
	 * @param maijiToken
	 * @return
	 */
	@PostMapping("checkPayPassword")
	public BaseDataResDto<Boolean> checkPayPassword(@RequestBody BaseDataReqDto<String> param,@RequestHeader("maijiToken") String maijiToken){
		logger.info("UserController.checkPayPassword ,param is {},maijiToken is {}", JSON.toJSONString(param),maijiToken);
		if(StringUtils.isBlank(maijiToken) || StringUtils.isBlank(param.getData()))return new BaseDataResDto<Boolean>(Status.PARAMETERERROR, "参数为空");
		// 获得用户id
		AppUser appUser = null;
		appUser = appUserService.checkToken(maijiToken);
		if (appUser == null) {
			logger.info("UserController.checkPayPassword,appUser is {}", JSON.toJSONString(appUser));
			return new BaseDataResDto(Status.PARAMETERERROR, "token 无效 ");
		}
		
		return userService.checkPayPassword(param,appUser.getUuId());
	}

	/**
	 * 开起麦吉之旅之后是否连续的记录体重
	 * @param maijiToken
	 * @return
	 */
	@PostMapping("isSuccessionRecordWeight")
	public BaseDataResDto<Boolean> isSuccessionRecordWeight(@RequestHeader("maijiToken")String maijiToken)throws Exception {
		
		return userService.isSuccessionRecordWeight(maijiToken);
	}

	/**
	 * 是否长时间未登录
	 * @param maijiToken
	 * @return
	 */
	@PostMapping("isLongTimeNotLogIn")
	public BaseDataResDto<Boolean> isLongTimeNotLogIn(@RequestHeader("maijiToken")String maijiToken)throws Exception{
		
		return userService.isLongTimeNotLogIn(maijiToken);
	}
	

}
